package cn.fan.web;

import cn.fan.constants.DataBaseConstants;
import cn.fan.pojo.FundMain;
import cn.fan.service.FundMainService;
import cn.fan.utils.MyPage;
import cn.fan.utils.R;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("fundmains")
public class FundMainController {

    @Autowired
    private FundMainService fundMainService;

    @GetMapping("test")
    public String test(){
        return "test!";
    }

    @GetMapping("get/{id}")
    public R get(@PathVariable("id") long id){
        FundMain fundMain = fundMainService.getById(id);
        if (fundMain == null)
            return R.error("无该基金数据");

        Map<String,Object> res =new HashMap<>();
        res.put("fundMain",fundMain);
        return R.ok(res);
    }

    @GetMapping("getByCode/{code}")
    public R getByCode(@PathVariable("code") String code){
        FundMain fundMain = fundMainService.getByCode(code);
        if (fundMain == null)
            return R.error("无该基金数据");

        Map<String,Object> res =new HashMap<>();
        res.put("fundMain",fundMain);
        return R.ok(res);
    }

    @GetMapping("select/{code}")
    public FundMain selectByCode(@PathVariable("code") String code){
        System.out.println("调用了select");
        return fundMainService.getByCode(code);
    }

    // 支持模糊添加
    @GetMapping("add/{code}")
    public R add(@PathVariable("code") String code){
//        fundMainService.add(code);
        fundMainService.synchro(code);
        return R.ok();
    }

    @PostMapping("add")
    public R add(@RequestBody FundMain fundMain){
        fundMainService.synchro(fundMain);
        return R.ok();
    }

    @GetMapping("delete/{id}")
    public R delete(@PathVariable("id") long id){
        int i = fundMainService.delete(id);
        return i> DataBaseConstants.NO_DATA ? R.ok() : R.error("无该基金数据");
    }

    @PostMapping("edit")
    public R edit(@RequestBody FundMain fundMain){
        int i = fundMainService.edit(fundMain);
        return i> DataBaseConstants.NO_DATA ? R.ok() : R.error("无该基金数据");
    }

    @PostMapping("list")
    public R list(@RequestBody FundMain fundMain){
        List<FundMain> list = fundMainService.list(fundMain);
        Map<String,Object> res = new HashMap<>();
        res.put("list",list);
        return R.ok(res);
    }

    // 暂时提供，待用户管理基金信息完善后取消该接口
    @GetMapping("list")
    public R listTest(){
        List<FundMain> list = fundMainService.list();
        Map<String,Object> res = new HashMap<>();
        res.put("list",list);
        return R.ok(res);
    }

    @PostMapping("list/{pageSize}/{currentPage}")
    public R page(@RequestBody FundMain fundMain,@PathVariable("pageSize") long pageSize,@PathVariable("currentPage") long currentPage){
        IPage<FundMain> page = fundMainService.page(fundMain,currentPage,pageSize);
        MyPage pagination=new MyPage(page);
        Map<String,Object> res = new HashMap<>();
        res.put("pagination",pagination);
        return R.ok(res);
    }
}
